Home:ALL Converter>Uppercase or Lowercase a specific character in a word "Java"

Uppercase or Lowercase a specific character in a word "Java"

Ask Time:2015-12-31T06:08:52         Author:Shah91n

Json Formatter

I have look into most of questions but I couldn't find how to uppercase or lowercase specific character inside a word.

Example:

String name = "Robert"

What if I would like to make "b" Uppercase and rest lowercase also how to make first letter Uppercase and rest lowercase?

Like "john" >> Output >> "John"...

I have toUppercase() and toLowercase(). They convert the whole text.

Also I tried to include charAt but never worked with me.

Author:Shah91n,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34538000/uppercase-or-lowercase-a-specific-character-in-a-word-java
bhooks :

You will need to take your string, take a substring of the specific character or characters you want to capitalize or lowercase, and then build a new string off of it.\n\nExample\n\nString test = \"JoHn\"; //make the H lowercase\ntest = test.substring(0,2) + test.substring(2,3).toLowercase() + test.substring(3);\n\n\nThe first substring gets all characters before the desired point, the second gets the desired character and lowercases it, and the final substring gets the rest of the string",
2015-12-30T22:12:24
Abdelhak :

You can use toCharArray() to capitalize the first letter like this:\n\nString name = \"robert\";\n\n// Convert String to char array.\nchar[] arr = name.toCharArray();\n\n// Modify first element in array.\narr[0] = Character.toUpperCase(arr[0]);\nString str = new String(arr);\nSystem.out.println(str);\n\n\nOutput:\n\nRobert\n\n\nAnd you want to make \"b\" Uppercase and rest lowercase like this:\n\n// Convert String to char array.\nchar[] arr2 = name.toCharArray();\n\n// Modify the third element in array.\narr2[2] = Character.toUpperCase(arr2[2]);\nString str2 = new String(arr2);\nSystem.out.println(str2);\n\n\nOutput:\n\nroBert\n",
2015-12-30T22:28:48
Pralhad :

//Try this...\n\nString str = \"Robert\";\n\nfor (int i = 0; i < str.length(); i++) {\n int aChar = str.charAt(i);\n\n // you can directly use character instead of ascii codes\n if (aChar == 'b') {\n aChar = aChar - 32;\n } else if (aChar >= 'A' && aChar <= 'Z') {\n aChar += 32 ;\n }\n\n System.out.print((char) aChar);\n}\n\n\n/*\nOutput will be- roBert \n\n*/",
2018-01-29T11:05:14
Lew Bloch :

I wouldn't use 'test.substring(2, 3).toLowerCase()' necessarily. 'Character.valueOf(test.charAt(2)).toUpperCase()' works. Also, the 'test.substring(0, 3)' is wrong; it should be 'test.substring(0, 2)'.",
2015-12-30T22:19:51
yy